<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_daily_report extends CI_Controller {

	public $data;

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Admin_daily_report_model');
	}

	public function index()
	{
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data from company table
		$daily_reports = $this->Admin_daily_report_model->get_all();
		$userdata = $this->session->userdata('userInfo');

		$data = array(
			'title' => 'Khai nghiệp | Thu chi hang ngay',
			'name' => $userdata['name'],
 			'daily_reports' => $daily_reports
		);

		$this->load->view('tmpl/header', $data);
		$this->load->view('tmpl/left', $data);
		$this->load->view('admin_daily_report_view', $data);
		$this->load->view('tmpl/footer');
	}

	public function add_new()
	{	
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
	
		//get data
		$created = $_POST['created'];
		$created = str_replace('/', '-', $created);
		$created = date('Y-m-d', strtotime($created));

		if(null != $created) {//if data not null

			$new_data = array(
				'created' => $created,
				'earn_total' => 0,
				'spend_total' => 0,
			);

			$id = $this->Admin_daily_report_model->add_new($new_data); //id = -1: fail, else 
			//success

			if($id != -1) { //insert success
				$rp = array('id' => $id);
				echo json_encode($rp);
			}else
				return false;
		}else
			return false;
	}

	//edit
	public function edit()
	{	
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data
		$id = $_POST['id'];
		$created = $_POST['created'];
		$created = str_replace('/', '-', $created);
		$created = date('Y-m-d', strtotime($created));

		if((null != $id) && (null != $created)) {//if data not null
			$cond = array(
				'id' => $id
			);

			$new_data = array(
				'created' => $created,
				'earn_total' => 0,
				'spend_total' => 0
			);
 
			//success
			if($this->Admin_daily_report_model->edit($new_data, $cond)) { //update success
				return true;
			}else
				return false;
		}else
			return false;
	}

	//delete
	public function delete()
	{
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data
		$id = $_POST['id'];
		if(null != $id) {
			$cond = array(
				'id' => $id
			);
			
			if($this->Admin_daily_report_model->delete($cond)) { //delete success
				return true;
			}else
				return false;

		}else 
			return false;
	}

	//edit
	public function edit_value_total()
	{	
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data
		$id = $_POST['id'];
		$earn_total = $_POST['earn_total'];
		$spend_total = $_POST['spend_total'];

		if((null != $id) && (null != $earn_total) && (null != $spend_total)) {//if data not null
			$cond = array(
				'id' => $id
			);

			$new_data = array(
				'earn_total' => $earn_total,
				'spend_total' => $spend_total
			);
 
			//success
			if($this->Admin_daily_report_model->edit($new_data, $cond)) { //update success
				return true;
			}else
				return false;
		}else
			return false;
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */